package com.jlb.demo.entity;

/**
 * 订单实体类 - 演示继承和业务逻辑
 * 继承BaseEntity，获得id、createTime、updateTime字段
 */
public class Order extends BaseEntity {
    
    /**
     * 用户ID
     */
    private Long userId;
    
    /**
     * 订单金额
     */
    private Double amount;
    
    /**
     * 订单状态：待支付、已支付、已发货、已完成、已取消
     */
    private String status;
    
    /**
     * 订单描述
     */
    private String description;
    
    /**
     * 支付方式
     */
    private String paymentMethod;
    
    /**
     * 订单状态枚举
     */
    public enum OrderStatus {
        PENDING_PAYMENT("待支付"),
        PAID("已支付"),
        SHIPPED("已发货"),
        COMPLETED("已完成"),
        CANCELLED("已取消");
        
        private final String displayName;
        
        OrderStatus(String displayName) {
            this.displayName = displayName;
        }
        
        public String getDisplayName() {
            return displayName;
        }
    }
    
    /**
     * 默认构造方法
     */
    public Order() {
        super();
        this.status = OrderStatus.PENDING_PAYMENT.getDisplayName();
    }
    
    /**
     * 带参数的构造方法
     */
    public Order(Long id, Long userId, Double amount, String description) {
        super(id);
        this.userId = userId;
        this.amount = amount;
        this.description = description;
        this.status = OrderStatus.PENDING_PAYMENT.getDisplayName();
    }
    
    /**
     * 更新订单状态 - 演示业务逻辑和状态流转
     * @param newStatus 新状态
     * @throws IllegalArgumentException 当状态流转不合法时抛出异常
     */
    public void updateStatus(String newStatus) {
        // 验证状态流转的合法性
        if (!isValidStatusTransition(this.status, newStatus)) {
            throw new IllegalArgumentException(
                String.format("订单状态不能从 '%s' 直接变为 '%s'", this.status, newStatus)
            );
        }
        
        String oldStatus = this.status;
        this.status = newStatus;
        setUpdateTime();
        
        System.out.println(String.format("订单 %d 状态从 '%s' 更新为 '%s'", getId(), oldStatus, newStatus));
    }
    
    /**
     * 判断状态流转是否合法
     * @param currentStatus 当前状态
     * @param newStatus 新状态
     * @return 是否合法
     */
    private boolean isValidStatusTransition(String currentStatus, String newStatus) {
        // 待支付 -> 已支付 或 已取消
        if (OrderStatus.PENDING_PAYMENT.getDisplayName().equals(currentStatus)) {
            return OrderStatus.PAID.getDisplayName().equals(newStatus) ||
                   OrderStatus.CANCELLED.getDisplayName().equals(newStatus);
        }
        
        // 已支付 -> 已发货 或 已取消
        if (OrderStatus.PAID.getDisplayName().equals(currentStatus)) {
            return OrderStatus.SHIPPED.getDisplayName().equals(newStatus) ||
                   OrderStatus.CANCELLED.getDisplayName().equals(newStatus);
        }
        
        // 已发货 -> 已完成
        if (OrderStatus.SHIPPED.getDisplayName().equals(currentStatus)) {
            return OrderStatus.COMPLETED.getDisplayName().equals(newStatus);
        }
        
        // 已完成和已取消状态不能再改变
        return false;
    }
    
    /**
     * 支付订单
     * @param paymentMethod 支付方式
     */
    public void pay(String paymentMethod) {
        if (!OrderStatus.PENDING_PAYMENT.getDisplayName().equals(this.status)) {
            throw new IllegalStateException("只有待支付的订单才能进行支付");
        }
        
        this.paymentMethod = paymentMethod;
        updateStatus(OrderStatus.PAID.getDisplayName());
        System.out.println(String.format("订单 %d 通过 %s 支付成功，金额：%.2f", getId(), paymentMethod, amount));
    }
    
    /**
     * 取消订单
     */
    public void cancel() {
        if (OrderStatus.COMPLETED.getDisplayName().equals(this.status)) {
            throw new IllegalStateException("已完成的订单不能取消");
        }
        
        updateStatus(OrderStatus.CANCELLED.getDisplayName());
        System.out.println(String.format("订单 %d 已取消", getId()));
    }
    
    /**
     * 计算折扣金额（VIP用户打9折）
     * @param isVip 是否VIP
     * @return 折扣后金额
     */
    public Double calculateDiscountedAmount(boolean isVip) {
        if (isVip) {
            return amount * 0.9;
        }
        return amount;
    }
    
    /**
     * 获取订单摘要
     * @return 订单摘要
     */
    public String getSummary() {
        return String.format("订单[ID=%d, 用户ID=%d, 金额=%.2f, 状态=%s, 描述=%s]", 
                getId(), userId, amount, status, description);
    }
    
    // Getter和Setter方法
    public Long getUserId() {
        return userId;
    }
    
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    
    public Double getAmount() {
        return amount;
    }
    
    public void setAmount(Double amount) {
        this.amount = amount;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getPaymentMethod() {
        return paymentMethod;
    }
    
    public void setPaymentMethod(String paymentMethod) {
        this.paymentMethod = paymentMethod;
    }
    
    @Override
    public String toString() {
        return "Order{" +
                "id=" + getId() +
                ", userId=" + userId +
                ", amount=" + amount +
                ", status='" + status + '\'' +
                ", description='" + description + '\'' +
                ", paymentMethod='" + paymentMethod + '\'' +
                ", createTime=" + getCreateTime() +
                ", updateTime=" + getUpdateTime() +
                '}';
    }
} 